﻿using System.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;


namespace AssetManageSystem.ViewModel.AssetManage.AssetConsumingVMs
{
    public partial class AssetConsumingListVM : BasePagedListVM<AssetConsuming_View, AssetConsumingSearcher>
    {
        protected override List<GridAction> InitGridAction()
        {
            return new List<GridAction>
            {
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-green"),
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-blue"),
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-8"),
                // this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-orange"),
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "AssetManage", dialogWidth: 800).SetButtonClass("layui-bg-red"),
                // this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "AssetManage", dialogWidth: 800).SetButtonClass("btn-col-4"),
                this.MakeStandardAction("AssetConsuming", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "AssetManage").SetButtonClass("btn-col-4"),
                //this.MakeAction("AssetConsuming","PrintDoc","打印","单据打印",GridActionParameterTypesEnum.SingleId,"AssetManage",dialogWidth: 800).SetShowInRow(true).SetHideOnToolBar(true).SetButtonClass("btn-col-1"),
            };
        }


        protected override IEnumerable<IGridColumn<AssetConsuming_View>> InitGridHeader()
        {
            return new List<GridColumn<AssetConsuming_View>>{
                this.MakeGridHeader(x => x.DocNumber),
                this.MakeGridHeader(x => x.UserName_view),
                this.MakeGridHeader(x => x.DepName_view),
                this.MakeGridHeader(x => x.OutDate),
                this.MakeGridHeader(x => x.AsName_view),
                this.MakeGridHeaderAction(width: 250)
            };
        }

        public override IOrderedQueryable<AssetConsuming_View> GetSearchQuery()
        {
            var query = DC.Set<AssetConsuming>()
                .CheckEqual(Searcher.AsUserId, x=>x.AsUserId)
                .CheckEqual(Searcher.UnitId, x=>x.UnitId)
                .CheckWhere(Searcher.SelectedAssetIdIDs,x=>DC.Set<AssetConsumMiddleTable>().Where(y=>Searcher.SelectedAssetIdIDs.Contains(y.AssetId)).Select(z=>z.ConsumId).Contains(x.ID))
                .Select(x => new AssetConsuming_View
                {
				    ID = x.ID,
                    DocNumber = x.DocNumber,
                    UserName_view = x.AsUser.UserName,
                    DepName_view = x.Unit.DepName,
                    OutDate = x.OutDate,
                    AsName_view = x.AssetId.Where(y=>y.Asset.IsValid==true).Select(y=>y.Asset.AsName).ToSepratedString(null,","), 
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class AssetConsuming_View : AssetConsuming{
        [Display(Name = "领用人")]
        public String UserName_view { get; set; }
        [Display(Name = "领用部门")]
        public String DepName_view { get; set; }
        [Display(Name = "资产名称")]
        public String AsName_view { get; set; }

    }
}
